using UnityEngine;

namespace Script.StateMachine
{
    public class ChaseState : IState
    {
        private FSM manager;
        private Parameter _parameter;
        
        public ChaseState(FSM manager)
        {
            this.manager = manager;
            _parameter = manager.parameter;
        }

        public void OnEnter()
        {
            manager.animator.Play("Walk");
        }
        public void OnUpdate()
        {
            manager.Flip(_parameter.playerTrans);
            if (_parameter.playerTrans) //当发现player时，开始追击
            {
                manager.transform.position = Vector2.MoveTowards(manager.transform.position,
                                                            _parameter.playerTrans.position,
                                                        _parameter.chaseSpeed * Time.deltaTime);
            }
            
            if (_parameter.isHit)
            {
                manager.TransitionState(StateType.Hit);
            }
            
            //当player超出视野（player的位置信息为空），或者enemy超出追击范围
            if (null ==_parameter.playerTrans
                || manager.transform.position.x < _parameter.chasePoints[1].position.x
                || manager.transform.position.x > _parameter.chasePoints[0].position.x)
            {
                manager.TransitionState(StateType.Idle);
            }
            
            //攻击范围内检测到玩家，检测攻击圆心是否在player图层上
            if (Physics2D.OverlapCircle(_parameter.attackPoint.position, _parameter.attackArea, _parameter.targetLayer))
            {
                manager.TransitionState(StateType.Attack);
            }

        }
        public void OnExit()
        {
            
        }
    }
}